package com.qhgrain.api.menurolepermission.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import tk.mybatis.mapper.entity.Example;

import com.paas.base.modules.utils.page.PageBean;
import com.qhgrain.api.menurolepermission.dao.PermissionMapper;
import com.qhgrain.api.menurolepermission.dao.RolePermissionMapper;
import com.qhgrain.api.menurolepermission.entity.Permission;
import com.qhgrain.api.menurolepermission.entity.RolePermission;
import com.qhgrain.api.menurolepermission.info.PermissionInfo;
import com.qhgrain.api.menurolepermission.service.IPermissionService;

@Service
@Transactional(rollbackFor = Exception.class)
public class PermissionServiceImpl implements IPermissionService{

	@Autowired
	private PermissionMapper permissionMapper;
	@Autowired
	private RolePermissionMapper rolePermissionMapper;
	
	@Override
	public void add(Permission permission) {
		this.permissionMapper.insert(permission);
		
	}

	@Override
	public void update(Permission permission) {
		this.permissionMapper.updateByPrimaryKey(permission);
		
	}

	@Override
	public void delete(List<String> ids) {
		//先删除权限表
		Example e1 = new Example(Permission.class);
		e1.createCriteria().andIn("id", ids);
		this.permissionMapper.deleteByExample(e1);
		//再删除角色-
		Example e2 = new Example(RolePermission.class);
		e2.createCriteria().andIn("permissionId", ids);
		this.rolePermissionMapper.deleteByExample(e2);
	}

	@Override
	public PageBean<PermissionInfo> queryPage(String id, String name, String type,
			String menuName, String appId, int pageNo, int pageSize)  {
		PageBean<PermissionInfo> page = new PageBean<PermissionInfo>(pageNo,pageSize);
		page.setData(this.permissionMapper.queryPage(id, name, type, menuName, appId, page.getFirst(), pageSize));
		page.setTotalCount(this.permissionMapper.queryPageCount(id, name, type, menuName, appId));
		return page;
	}

	@Override
	public PermissionInfo findInfo(String permissionId) {
		
//		return this.permissionMapper.selectByPrimaryKey(permissionId);
		return this.permissionMapper.findInfoById(permissionId);
	}

	@Override
	public Permission find(String permissionId) {
		
		return this.permissionMapper.selectByPrimaryKey(permissionId);
	}
	@Override
	public Permission queryByPvalue(String pvalue) {
		Example e = new Example(Permission.class);
		e.createCriteria().andEqualTo("pvalue",pvalue);
		List<Permission> list = this.permissionMapper.selectByExample(e);
		if(list == null || list.size()<=0){
			return null;
		}
		return list.get(0);
	}

	@Override
	public List<Permission> queryByMenus(List<String> menuIds) {
		Example e = new Example(Permission.class);
		e.createCriteria().andIn("menuId", menuIds);
		
		return this.permissionMapper.selectByExample(e);
	}

	@Override
	public List<PermissionInfo> queryByUserApp(String userId, String appId,String permissionType) {
		
		return this.permissionMapper.queryByUserApp(userId, appId, permissionType);
	}

	@Override
	public List<PermissionInfo> queryByRoleApp(String roleId, String appId,String permissionType) {
		
		return this.permissionMapper.queryByRoleApp(roleId, appId, permissionType);
	}

	
}
